
from wtforms import  StringField, IntegerField
from wtforms.validators import Email, Length, EqualTo
from ..forms import BaseForm
from utils import zlcache
from wtforms import ValidationError
from flask import g

class LoginForm(BaseForm):
     email = StringField(validators=[Email(message='请输入正确的邮箱格式')])
     password = StringField(validators=[Length(6, 20, message='请输入正确格式的密码')])
     #记住我
     remember = IntegerField()


class RestpwdForm(BaseForm):
     oldpwd = StringField(validators=[Length(6, 20, message='请输入正确格式的旧密码')])
     newpwd = StringField(validators=[Length(6, 20, message='请输入正确格式的新密码')])
     newpwd2 = StringField(validators=[EqualTo("newpwd", message='确认密码必须和新密码保持一致')])

class ResetEmailForm(BaseForm):
     email = StringField(validators=[Email(message='请输入正确格式的邮箱!')])
     captcha = StringField(validators=[Length(min=6, max=6, message='请输入正确长度的验证码')])

     def validate_captcha(self, field):
          captcha = field.data
          email = self.email.data
          #验证输入的验证码和缓存的验证码是否匹配
          captcha_cache = str(zlcache.get(email), encoding='utf-8')

          if not captcha_cache or captcha.lower() != captcha_cache.lower():
               raise ValidationError('邮箱验证码错误!')



     #验证邮箱是否为同一邮箱
     def validate_email(self, field):
          email = field.data
          user = g.cms_user
          if user.email == email:
               raise ValidationError('不能修改为相同的邮箱')